package permissions

import (
	"context"
	"errors"
	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"
	"product/common/datetime"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeletePermissionsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// delete permissions
func NewDeletePermissionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePermissionsLogic {
	return &DeletePermissionsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeletePermissionsLogic) DeletePermissions(req *types.DeletePermissionReq) (resp *types.DeletePermissionResp, err error) {

	ok, err := validDeletePermissionsInput(req)
	if !ok && err != nil {
		return &types.DeletePermissionResp{
			Message: err.Error(),
			Ok:      false,
		}, nil
	}
	fields := make(map[string]interface{})
	fields["IsDeleted"] = 1
	fields["UpdatedAt"] = datetime.GetNowTimestamp()
	_, err = l.svcCtx.PermissionsModel.ModifyPermissions(l.ctx, req.Id, fields)
	if err != nil {
		return &types.DeletePermissionResp{
			Message: "删除失败: " + err.Error(),
			Ok:      false,
		}, nil
	}
	return &types.DeletePermissionResp{
		Message: "删除成功",
		Ok:      true,
	}, nil
}

func validDeletePermissionsInput(req *types.DeletePermissionReq) (bool, error) {
	if req.Id <= 0 {
		return false, errors.New("id不能为空")
	}
	return true, nil
}
